{% extends "../base.html" %}

<!-- =================================================================== -->
<!-- Page onload ======================================================= -->
<!-- =================================================================== -->
{% block BaseHead %}
<script type="text/javascript">
GW.pageOnLoad = function() {
    GW.div.fullscreen( "idBB" );
    GW.initDialog();
    GW.log.log( 'developerload.html init' );    
    GW.wait.hide();
};
GW.initDialog = function(){
    GW.dialog = new YAHOO.widget.Dialog( "idDialog",
        {  
            postmethod: "none",
            fixedcenter : true,
            visible : false,
            hideaftersubmit: true,
            constraintoviewport: true,
            modal: false,
            buttons : [ { text:"Cancel", handler: function(){ this.cancel();}, isDefault:true },
                { text:"Execute", handler:GW.execute.execute }]
        }
    );
    GW.dialog.render();
    GW.dialog.show();
    YAHOO.util.Event.addListener("show", "click", GW.dialog.show, GW.dialog, true);
};
GW.execute = {
    data: null,                                                                 // the form data
    report: null,                                                               // the report element (div where report is displayed)
    cmdFlags: '',                                                               // get command flags
    objects: [],                                                                // objects to process
    next: 0,                                                                    // the next option to process
    execute: function(){                                                        // execution handler
            GW.dialog.hide();
            GW.execute.data = this.getData();
            GW.execute.report = document.getElementById( 'idReport' );
            GW.execute.report.innerHTML = 'Executing<br />' +
                'Flags:<br />' +
                'erase: ' + GW.execute.data.erase + '<br />' +
                'load: ' + GW.execute.data.load + '<br />' +
                'overwrite: ' + GW.execute.data.overwrite + '<br />';
            GW.execute.objects = GW.execute.data.options;
            if( GW.execute.objects[0] == 'All' ){
                GW.execute.objects = [ 'Tags', 'Colors', 'Markers', 'Users', 'Pages', 'Comments'];
            }
            GW.execute.next = 0;
            GW.wait.show( false, 'Executing' );
            if( GW.execute.data.erase ){
                GW.execute.doErase();
            } else if( GW.execute.data.load ){
                GW.execute.doLoad();
            }
    },
    doErase: function(){
        if( GW.execute.next >= GW.execute.objects.length ){
            if( !GW.execute.data.load ) {
                GW.wait.hide();
                return;
            }
            GW.execute.next = 0;
            GW.execute.doLoad();
            return;
        }
        var objDiv = document.getElementById("idReport");                       // scroll to the bottom of the report
        objDiv.scrollTop = objDiv.scrollHeight;
        GW.wait.show( false, 'Erasing ' + GW.execute.objects[GW.execute.next] );
        var cmd = 'erase=true&load=false&overwrite=false&object=' + GW.execute.objects[GW.execute.next];
        GW.execute.report.innerHTML = GW.execute.report.innerHTML +
            '<br />Erase all ' + GW.execute.objects[GW.execute.next] +
            //' --> cmd: ' + cmd + '<br />' +
            '<br />This may take a few minutes. Please be patient. Working ... <br />';
        GW.execute.next += 1;
        // Execute command
        var callback = {
            success: function(o){
                var msg = 'Command succeeded. Status:' + o.status + ' Text:' + o.statusText;
                GW.log.log( msg, 'info', 'developer' );
                GW.execute.report.innerHTML = GW.execute.report.innerHTML + o.responseText;
                GW.execute.doErase();
            },
            failure: function(o){
                var msg = 'Command failed. Status:' + o.status + ' Text:' + o.statusText;
                GW.log.log( msg, 'error', 'developer failed' );
                alert( 'Developer command failed. Check log!' );
                GW.execute.report.innerHTML = GW.execute.report.innerHTML +  'Developer command failed. Check log!<br />' + o.statusText + '<br />';
                GW.execute.doErase();
            },
            timeout: 60000
        };
        YAHOO.util.Connect.setDefaultPostHeader( false );
        YAHOO.util.Connect.initHeader( "Content-Type","application/json; charset=utf-8" );
        var request = YAHOO.util.Connect.asyncRequest('GET', '/gw/json/developer?' + cmd, callback );
    },
    doLoad: function(){
        if( GW.execute.next >= GW.execute.objects.length ){
            GW.wait.hide();
            return;
        }
        GW.wait.show( false, 'Loading ' + GW.execute.objects[GW.execute.next] );
        var objDiv = document.getElementById("idReport");                       // scroll to the bottom of the report
        objDiv.scrollTop = objDiv.scrollHeight;
        var cmd = '&erase=false' +
                '&overwrite=' + GW.execute.data.overwrite +
                '&load=' + GW.execute.data.load +
                '&object=' + GW.execute.objects[GW.execute.next];
        GW.execute.report.innerHTML = GW.execute.report.innerHTML +
            '<br />Load all ' + GW.execute.objects[GW.execute.next] +
            '<br />This may take a few minutes. Please be patient. Working ... <br />';
        GW.execute.next += 1;
        // Execute command
        var callback = {
            success: function(o){
                var msg = 'Command succeeded. Status:' + o.status + ' Text:' + o.statusText;
                GW.log.log( msg, 'info', 'developer' );
                GW.execute.report.innerHTML = GW.execute.report.innerHTML +  o.responseText;
                GW.execute.doLoad();
            },
            failure: function(o){
                var msg = 'Command failed. Status:' + o.status + ' Text:' + o.statusText;
                GW.log.log( msg, 'error', 'developer failed' );
                alert( 'Developer command failed. Check log!' );
                GW.execute.report.innerHTML = GW.execute.report.innerHTML +  'Developer command failed. Check log!<br />' + o.statusText + '<br />';
                GW.execute.doLoad();
            },
            timeout: 400000
        };
        YAHOO.util.Connect.setDefaultPostHeader( false );
        YAHOO.util.Connect.initHeader( "Content-Type","application/json; charset=utf-8" );
        var request = YAHOO.util.Connect.asyncRequest('GET', '/gw/json/developer?' + cmd, callback );
    }
};
</script>
{% endblock %}

<!-- =================================================================== -->
<!-- Page body ========================================================= -->
<!-- =================================================================== -->
{% block BaseBody %}
<div id="idBB" style="overflow: auto;" >
    <div style="padding-top: 0.5em; padding-left: 0.5em;" >
        <button id="show" >Show dialog </button>
    </div>
    <div id="idReport" style="padding-left: 1em; padding-top: 0.5em;" ><!-- report goes here --></div>
</div>

<style>
    label {
        width: 4em;
        float: left;
        text-align: right;
        margin-right: 0.5em;GW.execute
        diaplay: block;
    }
    select input {
        margin-left: 4.5em;
    }
    .clear { clear:both; }
</style>

<div id="idDialog" style="visibility:hidden" >
    <div class="hd">Please select</div>
    <div class="bd">
        <form>
            <label for="erase">Erase</label><input type="checkbox" id="erase" name="erase" value="erase"><div class="clear"></div>
            <label for="load">Load</label><input type="checkbox" id="load" name="load" value="load"><div class="clear"></div>
            <label for="overwrite">Overwrite</label><input type="checkbox" id="overwrite" name="overwrite" value="overwrite"><div class="clear"></div>
            <label for="opertions" style="vertical-align: top;">Options</label>
            <select multiple id="options" name="options" size="7" ><div class="clear"></div>
                <option value="All" selected="selected">All</option>
                <option value="Tags">Tags</option>
                <option value="Colors">Colors</option>
                <option value="Markers">Markers</option>
                <option value="Users">Users</option>
                <option value="Pages">Pages</option>
                <option value="Comments">Comments</option>
            </select><br />
        </form>
    </div>
</div>

{% endblock %}